/*
 * cadProdutoJInternalFrame.java
 * Created on 27/10/2009, 21:03:56
 */
package br.com.fatecpg.zanotti.jframes.consultas;

import br.com.fatecpg.zanotti.acessobanco.conTableCliente;
import br.com.fatecpg.zanotti.acessobanco.conTableEntrega;
import br.com.fatecpg.zanotti.acessobanco.conTableVenda;
import br.com.fatecpg.zanotti.jframes.cadastros.cadVendaJInternalFrame;
import br.com.fatecpg.zanotti.modelo.Cliente;
import br.com.fatecpg.zanotti.modelo.Pedido;

/**
 * @author Luiz Zanotti
 */
public class conEntregaJInternalFrame extends javax.swing.JInternalFrame {

    public conEntregaJInternalFrame() {

        initComponents();

        conEntregajTable.getColumnModel().getColumn(1).setMaxWidth(90);
        conEntregajTable.getColumnModel().getColumn(1).setMinWidth(90);
        conEntregajTable.getColumnModel().getColumn(2).setMaxWidth(70);
        conEntregajTable.getColumnModel().getColumn(2).setMinWidth(70);

    }

    public void atualizaTable() {
        conEntregajTable.removeAll();
        conTableEntrega con = new conTableEntrega();
        conEntregajTable = con.ConsultaTabelaToda(conEntregajTable);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        alterarjButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        conEntregajTable = new javax.swing.JTable();
        buscarjTextField = new javax.swing.JTextField();
        nome_cpfjComboBox = new javax.swing.JComboBox();
        buscarjButton = new javax.swing.JButton();
        pesquisarjLabel = new javax.swing.JLabel();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Consulta de Entregas");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        alterarjButton.setText("Alterar");
        alterarjButton.setEnabled(false);
        alterarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarjButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(alterarjButton, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(alterarjButton)
                .addContainerGap(183, Short.MAX_VALUE))
        );

        conEntregajTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Cliente", "Data Entrega", "Código"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        conEntregajTable.setCellSelectionEnabled(false);
        conEntregajTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(conEntregajTable);
        conEntregajTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        conEntregajTable.getColumnModel().getColumn(0).setResizable(false);
        conEntregajTable.getColumnModel().getColumn(1).setResizable(false);
        conEntregajTable.getColumnModel().getColumn(2).setResizable(false);

        nome_cpfjComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cliente", "Venda" }));

        buscarjButton.setText("Buscar");
        buscarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarjButtonActionPerformed(evt);
            }
        });

        pesquisarjLabel.setText("Pesquisar por:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(pesquisarjLabel)
                        .addGap(6, 6, 6)
                        .addComponent(buscarjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nome_cpfjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buscarjButton)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buscarjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pesquisarjLabel)
                    .addComponent(nome_cpfjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buscarjButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buscarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarjButtonActionPerformed
        conEntregajTable.removeAll();

        if (nome_cpfjComboBox.getSelectedItem() == "Cliente") {
            conTableEntrega con = new conTableEntrega();
            conEntregajTable = con.ConsultaTabelaPorCliente(conEntregajTable, buscarjTextField.getText());
        } else {
            
                conTableEntrega con = new conTableEntrega();
                conEntregajTable = con.ConsultaTabelaPorVenda(conEntregajTable, buscarjTextField.getText());
            } 
        
    }//GEN-LAST:event_buscarjButtonActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        atualizaTable();
    }//GEN-LAST:event_formInternalFrameOpened

    private void alterarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarjButtonActionPerformed
      if(conEntregajTable.getSelectedRow()<0){
javax.swing.JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada!", "Atenção", 2);}
      else{

        cadVendaJInternalFrame.getInstancia().limpaTudo();
        conTableVenda con = new conTableVenda();
        Pedido ped = new Pedido();
//        Contas_receber contas = new Contas_receber();

        ped = con.ConsultaPedidoPorLinha(conEntregajTable.getValueAt(conEntregajTable.getSelectedRow(), 2).toString());
//        contas = con.ConsultaCONTAS_RECEBERPorLinha(conVendajTable.getValueAt(conVendajTable.getSelectedRow(), 1).toString());

        cadVendaJInternalFrame.getInstancia().setObjPedido(ped);

        cadVendaJInternalFrame.getInstancia().setCliente(ped.getCPFCLIENTE(), Integer.parseInt(ped.getCLI_CDLOJA()));
        cadVendaJInternalFrame.getInstancia().setDataPGjFormattedTextField(ped.getDTPGTO());

        cadVendaJInternalFrame.getInstancia().setNFjTextField(ped.getCDNOTAFISCAL());

        Cliente cli = new conTableCliente().ConsultaObjetoPorCDCLIENTE(ped.getCPFCLIENTE());

        cadVendaJInternalFrame.getInstancia().setNomeClientejTextField(cli.getNMCLIENTE());
        cadVendaJInternalFrame.getInstancia().setEnd1jTextArea(cli.getNMENDLOGRADOURO() + ", " + Cliente.getInstancia().getNMENDNUMERO() + " - " + Cliente.getInstancia().getNMENDCOMP() + " - " + Cliente.getInstancia().getNMENDBAIRRO() + ", " + Cliente.getInstancia().getNMENDCIDADE() + " \n CEP: " + Cliente.getInstancia().getNMENDCEP());


        if (ped.getDTENTREGA() != null) {
            cadVendaJInternalFrame.getInstancia().setjCheckBox(true);
            cadVendaJInternalFrame.getInstancia().setDataEntregajFormattedTextField(ped.getDTENTREGA());
        } else {
            cadVendaJInternalFrame.getInstancia().setjCheckBox(false);
        }

        if (ped.getALT_ENDERECO() != null) {
            cadVendaJInternalFrame.getInstancia().setjRadioButton(false);
            cadVendaJInternalFrame.getInstancia().setEnd2jTextArea(ped.getALT_ENDERECO());
        } else {
            cadVendaJInternalFrame.getInstancia().setjRadioButton(true);
        }
        cadVendaJInternalFrame.getInstancia().setIcentreguejCheckBox(ped.getICENTREGUE());

        cadVendaJInternalFrame.getInstancia().setjTable1(con.ConsultaTabelaPedido_tem_Produto(cadVendaJInternalFrame.getInstancia().getjTable1(), ped.getCDVENDA()));


        //Envia tabela preenchida com produtos


cadVendaJInternalFrame.getInstancia().setAlterarVenda(true);
        cadVendaJInternalFrame.getInstancia().setVisible(true);
        this.setVisible(false);


      }
    }//GEN-LAST:event_alterarjButtonActionPerformed

        private static conEntregaJInternalFrame p;

    public static conEntregaJInternalFrame getInstancia() {
        if (p == null) {
            p = new conEntregaJInternalFrame();
        }
        return p;
    }



    public void setAlterarButton(boolean b){
alterarjButton.setEnabled(b);}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarjButton;
    private javax.swing.JButton buscarjButton;
    private javax.swing.JTextField buscarjTextField;
    private javax.swing.JTable conEntregajTable;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox nome_cpfjComboBox;
    private javax.swing.JLabel pesquisarjLabel;
    // End of variables declaration//GEN-END:variables
}
